package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangqi
 * @date 2023/12/27 10:13
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("分类分页查询")
    @GetMapping("/category/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("正在分页查询分类：{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("添加分类")
    @PostMapping("/category")
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("添加分类：{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @ApiOperation("禁用启用分类")
    @PostMapping("/category/status/{status}")
    public Result status(@PathVariable Integer status, Long id){
        log.info("分类禁用、启用 status={},id={}",status,id);
        categoryService.setStatus(status,id);
        return Result.success();
    }


    @DeleteMapping("/category")
    public Result delete(Long id){
        log.info("删除分类：{}",id);
        categoryService.delete(id);
        return Result.success();
    }

    @PutMapping("/category")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类：{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @GetMapping("/category/list")
    public Result<List<Category>> list(Integer type){
        log.info("根据类型查询分类：type={}",type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
